package w3_c2_queue;

/**
 * 双端队列。Double End QUEue.
 * @param <E>
 */
public class Deque<E> {
    /**
     * front指示第一个存在的头元素；tail指示尾元素的后一个元素（第一个空元素）；size是元素个数。
     */
    E[] data;
    int front, tail, size;

    public Deque(int capacity) {
        data = (E[]) new Object[capacity];
        front = tail = size = 0;
    }

    public Deque() {
        this(10);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return getSize() == getCapacity();
    }

    private void resize(int capacity) {
        E[] newData = (E[]) new Object[capacity];
        int i;
        for (i = 0; i < getSize(); i++) {
            newData[i] = data[(i + front) % getCapacity()];
        }
        front = 0;
        tail = i;
        data = newData;
    }

    /**
     * 从头部添加元素。
     * @param e
     */
    public void addFront(E e) {
        resize_helper();
        front = (front + getCapacity() - 1) % getCapacity();
        data[front] = e;
        size++;
    }

    public void addLast(E e) {
        resize_helper();
        data[tail] = e;
        tail = (tail + getCapacity() + 1) % getCapacity();
        size++;
    }

    /**
     * 从尾部删除元素。
     * @return
     */
    public E removeLast() {
        if (isEmpty())
            throw new IllegalArgumentException("Cannot remove from empty deque.");
        int lastOneIndex = (tail + getCapacity() - 1) % getCapacity();
        E res = data[lastOneIndex];
        data[lastOneIndex] = null;
        tail = lastOneIndex;
        size--;
        resize_helper();
        return res;
    }

    public E removeFront() {
        if (isEmpty())
            throw new IllegalArgumentException("Cannot remove from empty deque.");
        int firstOneIndex = front;
        E res = data[firstOneIndex];
        data[firstOneIndex] = null;
        front = (getCapacity() + firstOneIndex + 1)%getCapacity();
        size--;
        resize_helper();
        return res;
    }

    /**
     * 判断是否需要重新分配数组空间。
     */
    private  void resize_helper(){
        if(isFull())
            resize( 2 * getCapacity());
        else if(getSize() < getCapacity() / 4 && getCapacity() /2 != 0)
            resize(getCapacity() /2);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("LooqQueue, size = %d, capacity = %d\n", getSize(), getCapacity()));
        res.append("front [");
        for (int i = 0; i < getSize(); i++) {
            res.append(data[(i + front) % data.length]);
            if (i != getSize() - 1)
                res.append(", ");
        }
        res.append("] tail\n");
        return res.toString();
    }

    public static void main(String[] args) {
        Deque<Integer> deque = new Deque<>();
        for (int i = 0; i < 19; i++) {
            if (i % 2 == 0)
                deque.addLast(i);
            else
                deque.addFront(i);
            System.out.println(deque);
        }

        for (int i = 0; i < 19; i++) {
            if( i % 2 == 0)
                deque.removeFront();
            else
                deque.removeLast();
            System.out.println(deque);
        }
    }
}
